<template>
  <div class="VlonTextarea" v-if="item">
    <span class="left">{{ item.title }}</span>
    <div class="right">
      <VlonTinymceEditor
        ref="vlonTinymceEditor"
        v-if="value!==undefined&&value!==null"
        @tinymceContentUpdate="tinymceContentUpdate"
        :rightIndex="rightIndex"
        :catalogIndex="catalogIndex"
        :id="id"
        :content="value"
      ></VlonTinymceEditor>
    </div>
  </div>
</template>
<script>
import { mapState } from "vuex";
import VlonTinymceEditor from "@/pages/components/VlonTinymceEditor";
export default {
  name: "VlonTextarea",
  data() {
    return {
      value: null,

      //为了防止页面初始化时，触发不必要的通知更新，此处设置标识符，初始化后2秒置为true
      needUpdate: false,
    };
  },
  created() {},
  props: ["item", "rightIndex", "catalogIndex", "id"],
  components: { VlonTinymceEditor },
  mounted() {
    this.$nextTick(() => {
      this.init();
    });
    setTimeout(() => {
      this.needUpdate = true;
    }, 1000);
  },
  methods: {
    init() {
      if (this.item) {
        this.value = null;
        this.$nextTick(() => {
          let oItem = JSON.parse(JSON.stringify(this.item));
          this.value = oItem.value || oItem.default||""; //console.log(this.value);
        });
      }
    },
    //编辑器内容发生变化时触发
    tinymceContentUpdate(v) {
      if (this.needUpdate) {
        //标识是否不需要更新Main中的组件
        let needUpdateMainChild = true;
        // if (this.item.ename === "pageName") {
        //     needUpdateMain = false;
        // }
        //更新给直属父级
        this.$emit(
          "updateParent",
          JSON.stringify({
            value: v,
            ename: this.item.ename,
            rightIndex: this.rightIndex,
            catalogIndex: this.catalogIndex,
            needUpdateMainChild: needUpdateMainChild,
            needUpdateMain: false,
          })
        );
      }
    },
  },
  computed: mapState(["decorate"]),
  watch: {},
};
</script>
<style scoped lang="scss">
.VlonTextarea {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  padding: 5px 0px;
  box-sizing: border-box;
  .left {
    width: 100px;
    display: flex;
    justify-content: flex-end;
    padding-right: 10px;
    box-sizing: border-box;
    height: 40px;
    align-items: center;
    font-size: 12px;
  }
  .right {
    display: flex;
    align-items: center;
    width: calc(100% - 100px);
  }
}
</style>